package com.secsys.service.impl;



import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.secsys.dto.BbsReplyDTO;
import com.secsys.model.TbBbsPost;
import com.secsys.model.TbBbsReply;
import com.secsys.model.TbSysUser;
import com.secsys.service.ReplyService;
import com.secsys.util.DateFormatUtils;
/**
 * @Description 回帖业务逻辑接口实现类
 * @author LiusQ 
 * @Date 2011-4-8 20:37
 * @version 1.0
 *
 */
 
public class ReplyServiceImpl extends ServiceImpl<TbBbsReply, Integer> implements
		ReplyService {
      
	@Override
	public boolean auditReply(String postNo, String replyNo, String state) {
		// TODO Auto-generated method stub
		return false;
	}
    
	/**
	 * 
	 * @Description  回帖信息存储
	 */
	public void replyPost(TbBbsReply reply){
	   	dao.save(reply);
	}
	 /**
	  *  
	  * @Description  通过post Id查询其回复
	  * @param postId
	  * @return
	  */
	public List<BbsReplyDTO> getReplyById(TbBbsPost post){
		  String hql="from TbBbsReply reply where reply.tbBbsPost=?";
		  List<BbsReplyDTO> conList=null;
		List<TbBbsReply> reply=this.dao.find(hql, new Object[]{post});
		if(reply!=null&&reply.size()!=0){
		  conList=new ArrayList<BbsReplyDTO>();
			forEach(reply, conList);
		}
		return conList;
	}; 
  
	 /**
	  *  
	  * @Description   
	  * @param postId
	  * @return
	  */
	public List<BbsReplyDTO> getReplyInfo(TbBbsPost post,PostServiceImpl postServiceImpl){
		  String hql="from TbBbsReply reply where reply.tbBbsPost=?";
		  List<BbsReplyDTO> conList=null;
		List<TbBbsReply> reply=this.dao.find(hql, new Object[]{post});
		if(reply!=null&&reply.size()!=0){
		  conList=new ArrayList<BbsReplyDTO>();
			forEach(reply, conList,postServiceImpl);
		}
		return conList;
	}; 
	
	/**
	 * 
	 * @Description  pojo集合转换成dto集合
	 * @param list
	 * @param conList
	 */
	private void forEach(List<TbBbsReply> list, List<BbsReplyDTO> conList) {
		 BbsReplyDTO replyDTO=null;
		 try{
         for(TbBbsReply reply: list){
        	  replyDTO=new BbsReplyDTO();
        	  replyDTO.setContent(reply.getContent());
        	  replyDTO.setTitle(reply.getTitle());
        	  replyDTO.setReplyTime(DateFormatUtils.convertDateTimeToString(reply.getReplyTime()));
        	  replyDTO.setAuthor(reply.getTbSysUser().getUserName());
        	  replyDTO.setPostId(reply.getTbBbsPost().getId().toString());
        	  replyDTO.setStatue(reply.getStatue());
        	  replyDTO.setModuleName(reply.getTbBbsPost().getTbBbsModule().getModuleName());
        	  replyDTO.setRoleName(reply.getTbSysUser().getTbSysRole().getRole());  //得到回帖人的角色
        	  replyDTO.setPostCount(reply.getTbSysUser().getTbBbsPosts().size()); //得到回帖人的发帖数量
        	  replyDTO.setReplyCount(reply.getTbSysUser().getTbBbsReplies().size()); //得到回帖人的回帖数量
        	  replyDTO.setReplyId(reply.getTbSysUser().getId()); //得到回帖人的数据库标识
        	  if(reply.getTbSysUser().getPhoto()!=null){    //用户照片路径
        	  replyDTO.setPhoto(reply.getTbSysUser().getPhoto());
        	  }  
        	  
        	  conList.add(replyDTO);
         
         }}catch (Exception e) {
			logger.info(e.getMessage());
			 e.printStackTrace();
		}
	}
   
	/**
	 * 
	 * @Description  pojo集合转换成dto集合
	 * @param list
	 * @param conList
	 */
	private void forEach(List<TbBbsReply> list, List<BbsReplyDTO> conList,PostServiceImpl postServiceImpl) {
		 BbsReplyDTO replyDTO=null;
		 try{
         for(TbBbsReply reply: list){
        	  replyDTO=new BbsReplyDTO();
        	  replyDTO.setContent(reply.getContent());
        	  replyDTO.setRepId(reply.getId().toString());
        	  replyDTO.setTitle(reply.getTitle());
        	  replyDTO.setReplyTime(DateFormatUtils.convertDateTimeToString(reply.getReplyTime()));
        	  replyDTO.setAuthor(reply.getTbSysUser().getUserName());
        	  replyDTO.setPostId(reply.getTbBbsPost().getId().toString());
        	  replyDTO.setStatue(reply.getStatue());
        	  replyDTO.setModuleName(reply.getTbBbsPost().getTbBbsModule().getModuleName());
        	  replyDTO.setRoleName(reply.getTbSysUser().getTbSysRole().getRole());  //得到回帖人的角色
        	  replyDTO.setPostCount(reply.getTbSysUser().getTbBbsPosts().size()); //得到回帖人的发帖数量
        	  replyDTO.setReplyCount(reply.getTbSysUser().getTbBbsReplies().size()); //得到回帖人的回帖数量
        	  replyDTO.setReplyId(reply.getTbSysUser().getId()); //得到回帖人的数据库标识
        	  System.out.println(replyDTO.getReplyId()+"******************");
        	  if(reply.getTbSysUser().getPhoto()!=null){    //用户照片路径
        	  replyDTO.setPhoto(reply.getTbSysUser().getPhoto());
        	  }  
        	 int count= postServiceImpl.getDigestCount(reply.getTbSysUser().getId(), true);  //得到回帖用户的精华帖
        	  replyDTO.setDigestNum(count);
        	 conList.add(replyDTO);
         
         }}catch (Exception e) {
			logger.info(e.getMessage());
			 e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @Description  通过postId、lastReplyTime查的情况 
	 * @param postId
	 * @param lastReplyTime
	 * @return
	 */
	public TbBbsReply queryReplyByPostId(TbBbsPost  post,Date lastReplyTime){
		 System.out.println("TbBbsPost:"+post.getId()+"lastReplyTime:"+lastReplyTime);
		String hql="from TbBbsReply reply  where reply.tbBbsPost=? and reply.replyTime=?";
	 List list=dao.find(hql, new Object[]{post,lastReplyTime});
	 TbBbsReply reply=null;
	 if(list!=null&&list.size()!=0){
		 reply=(TbBbsReply) list.get(0);
	 }
	 return reply;
	}
	
	
	public TbBbsReply queryReplyByPostId(TbBbsPost  post){
		String hql="from TbBbsReply reply  where reply.tbBbsPost=? order by reply.replyTime Desc";
	 List list=dao.find(hql, new Object[]{post});
	 TbBbsReply reply=null;
	/** if(list!=null&&list.size()!=0){
		 reply=(TbBbsReply) list.get(0);
	 }
	 **/
	 return reply;
	}
	 
	/**
	 * 
	 * @Description  通过本人的回帖数 
	 * @return
	 */
	 public List<BbsReplyDTO> queryReplyByAuthor(TbSysUser tbSysUser){
		 String hql="from TbBbsReply reply  where reply.tbSysUser=? order by reply.replyTime Desc";
		 List list=dao.find(hql, new Object[]{tbSysUser});
		 System.out.println("回帖数："+list.size());
		 List conList=null;
		 if(list!=null&&list.size()!=0){
			 conList=new ArrayList();
			 forEach(list, conList);
		 }
		 return conList;
	 }
	
	
	/**
	 * @Description 获得帖子的回帖总数 
	 * @param post
	 * @return
	 */
   public Integer getCount(TbBbsPost post){
	    String hql="from TbBbsReply reply  where reply.tbBbsPost=?";
	   Integer count=dao.find(hql, new Object[]{post}).size();
	   return count;
   }

   /**
    * 
    * @Description 屏蔽回帖操作 
    * @param reply
    */
   public void noShowReplyInfo(TbBbsReply reply){
	   dao.update(reply);
   }
   
   
   /**
	 * 
	 * @Description  查询日期查询帖子数,并按照指定的顺序查询 
	 * @return
	 */
	public  List  queryReplyByDate(Date startDate,Date endDate,int moduleId){
		  String hql="from TbBbsReply reply where  reply.tbBbsPost.tbBbsModule.id=? and reply.replyTime  between ? and ?";
		  List   dayPostList=dao.find(hql,new Object[]{moduleId,startDate,endDate});
		  return dayPostList!=null&&dayPostList.size()>0?dayPostList:null;
	}
   
	/**
	 * 
	 * @Description  查询指定时间段的帖子 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public Integer totalCountByDate(Date startDate,Date endDate){
		String hql="select count(*) from TbBbsReply reply where  reply.replyTime  between ? and ?";
	    List list= dao.find(hql, new Object[]{startDate,endDate});
	    return list!=null&&list.size()>0?((Long)list.get(0)).intValue():0;
	}
	/**
	 * 
	 * @Description  查询指定时间段的帖子 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public Integer totalCountByModuleAndDate(Date startDate,Date endDate,String parentCode){
		String hql="select count(*) from TbBbsReply reply where reply.tbBbsPost.tbBbsModule.parent=? and reply.replyTime  between ? and ?";
	    List list= dao.find(hql, new Object[]{parentCode,startDate,endDate});
	    return list!=null&&list.size()>0?((Long)list.get(0)).intValue():0;
	}
	
	/**
	 * 
	 * @Description 利用sql语句 统计某日最高帖子量
	 * @return
	 */
	 public int totalMaxCountByDate(){
		    String sql="select max(tm.total)  dayMaxCount from( select count(*) total from  (SELECT  DATE_FORMAT(reply_time,'%Y-%m-%d') dateName, id FROM tb_bbs_reply t) t1,tb_bbs_reply t2 where t1.id=t2.id group by t1.dateName) tm;";
		     List  list=dao.getCount(sql);
		    return  list!=null&&list.size()>0?((BigInteger)list.get(0)).intValue():0;
	 }
	 
	 /**
	  * 
	  * @Description  所有回帖总数统计
	  * @return
	  */
	 public Integer allReplyCount(){
		  return dao.count(); 
		 
	 }
	 
	  /**
	   * 
	   * @Description 通过模块名查询回帖总数
	   * @param parentCode
	   * @return
	   */
		public Integer totalCountByModule(String parentCode){
			String hql="select count(*) from TbBbsReply reply where reply.tbBbsPost.tbBbsModule.parent=?";
		    List list= dao.find(hql, new Object[]{parentCode});
		    return list!=null&&list.size()>0?((Long)list.get(0)).intValue():0;
		}
	 
}
